package com.shui_shui.compass;

import com.shui_shui.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;

public class CompassHome extends View {
	private int bearing;
	private Bitmap bmp;

	public CompassHome(Context context) {
		super(context);
	}

	public CompassHome(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CompassHome(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public float getBearing() {
		return bearing;
	}

	public void setBearing(float bearing) {
		this.bearing = (int) bearing;
		sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
	}

	@SuppressLint({ "DrawAllocation", "DrawAllocation" })
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int px = getMeasuredWidth();
		int py = getMeasuredHeight();
		
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.homeeast);
		bmp = Bitmap.createScaledBitmap(bmp, px, py, false);
		
		canvas.rotate(-bearing, px/2, py/2);
		canvas.drawBitmap(bmp, 0, 0, null);
		canvas.restore();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int measureWidth = measure(widthMeasureSpec);
		setMeasuredDimension(measureWidth, measureWidth);

	}

	private int measure(int measureSpec) {
		int result = 0;
		// Decode the measurement specifications.
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize = MeasureSpec.getSize(measureSpec);
		if (specMode == MeasureSpec.UNSPECIFIED) {
			// Return a default size of 200 if no bounds are specified.
			result = 200;
		} else {
			// As you want to fill the available space
			// always return the full available bounds.
			result = specSize;
		}
		return result;

	}

}
